<template>
  <el-dialog
    :model-value="dialogVisible"
    @update:model-value="handleUpdateVisible"
    title="查看标签"
    width="800px"
    class="tag-dialog"
    :close-on-click-modal="false"
  >
    <div class="tag-content-container">
      <!-- 左侧标签列表 -->
      <div class="left-sidebar">
        <div class="tab-container">
          <div 
            v-for="(tag, index) in tagList" 
            :key="index"
            class="tab-item" 
            :class="{ active: activeTagIndex === index }"
            @click="handleTabClick(index)"
          >
            <span class="tab-text">{{ tag.tagNo }}</span>
          </div>
        </div>
      </div>

      <!-- 中间分割线 -->
      <div class="divider"></div>

      <!-- 右侧内容区域 -->
      <div class="right-content">
        <div v-if="currentTag" class="form-content">
          <el-descriptions :column="1" border class="custom-descriptions">
            <el-descriptions-item label="型号" class-name="my-content">
              <span>{{ currentTag.model || '-' }}</span>
            </el-descriptions-item>
            
            <el-descriptions-item label="出厂地" class-name="my-content">
              <span>{{ currentTag.factoryLocation || '-' }}</span>
            </el-descriptions-item>
            
            <el-descriptions-item label="公司" class-name="my-content">
              <span>{{ currentTag.company || '-' }}</span>
            </el-descriptions-item>
            
            <el-descriptions-item label="二维码" class-name="my-content">
              <div class="qrcode-container">
                <div class="qrcode-placeholder" v-if="!currentTag.qrcode">
                  二维码图片
                </div>
                <img v-else :src="currentTag.qrcode" alt="二维码" class="qrcode-image" />
              </div>
            </el-descriptions-item>
          </el-descriptions>
        </div>
      </div>
    </div>

    <!-- 底部按钮 -->
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleDownload" v-if="currentTag">下载</el-button>
        <el-button @click="handleClose">关闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'

// 定义props
const props = defineProps({
  visible: {
    type: Boolean,
    required: true
  },
  rowData: {
    type: Object,
    default: () => ({})
  }
})

// 定义emits
const emit = defineEmits(['update:visible', 'close'])

// 对话框可见性
const dialogVisible = computed({
  get: () => props.visible,
  set: (val) => emit('update:visible', val)
})

// 当前激活的标签索引
const activeTagIndex = ref(0)

// 标签列表（模拟数据）
const tagList = ref([
  { tagNo: 'ZBM001-1' },
  { tagNo: 'ZBM001-2' }
])

// 当前标签数据
const currentTag = computed(() => {
  if (tagList.value.length === 0 || activeTagIndex.value < 0) {
    return null
  }
  return {
    ...tagList.value[activeTagIndex.value],
    model: 'XH001',
    factoryLocation: '湖北十堰',
    company: '某某公司',
    qrcode: '' // 实际项目中应该是二维码图片地址
  }
})

// 监听行数据变化
watch(() => props.rowData, (newData) => {
  if (newData && Object.keys(newData).length > 0) {
    // 这里可以根据行数据加载对应的标签信息
    // 现在使用模拟数据
  }
}, { deep: true, immediate: true })

// 处理标签点击
const handleTabClick = (index) => {
  activeTagIndex.value = index
}

// 处理可见性更新
const handleUpdateVisible = (val) => {
  emit('update:visible', val)
  if (!val) {
    activeTagIndex.value = 0
  }
}

// 处理下载
const handleDownload = () => {
  ElMessage.success('下载功能待实现')
  // 实际项目中这里应该实现标签下载逻辑
}

// 处理关闭
const handleClose = () => {
  emit('update:visible', false)
  activeTagIndex.value = 0
  emit('close')
}
</script>

<style scoped>
/* 对话框样式 */
:deep(.tag-dialog .el-dialog__header) {
  background-color: #f5f7fa;
  padding: 16px 20px;
  border-bottom: 1px solid #ebeef5;
}

:deep(.tag-dialog .el-dialog__title) {
  font-size: 16px;
  font-weight: 500;
}

:deep(.tag-dialog .el-dialog__body) {
  padding: 0;
  max-height: 70vh;
  overflow: hidden;
}

:deep(.tag-dialog .el-dialog__footer) {
  padding: 16px 20px;
  border-top: 1px solid #ebeef5;
  background-color: #fafafa;
}

/* 主要内容容器 - 强制左右布局 */
.tag-content-container {
  display: flex !important;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  overflow: hidden;
  min-height: 500px;
}

/* 左侧标签列表样式 - 固定宽度 */
.left-sidebar {
  width: 180px !important;
  background-color: #f5f7fa;
  border-right: 1px solid #dcdfe6;
  flex-shrink: 0;
  overflow-y: auto;
}

.tab-container {
  display: flex;
  flex-direction: column;
}

.tab-item {
  padding: 12px 20px;
  cursor: pointer;
  transition: all 0.3s;
  border-left: 3px solid transparent;
  border-bottom: 1px solid #e4e7ed;
}

.tab-item:hover {
  background-color: #e6f2ff;
}

.tab-item.active {
  background-color: #2457d7 !important;
  color: #ffffff !important;
  border-left-color: #1890ff;
}

.tab-text {
  font-size: 14px;
  font-weight: 500;
}

/* 右侧内容区域 - 确保正确显示 */
.right-content {
  flex: 1;
  padding: 20px;
  background-color: #ffffff;
  overflow-y: auto;
}

/* 表单内容区域 */
.form-content {
  width: 100%;
}

/* 描述列表样式 - 按照customerInfo.vue的样式调整 */
.custom-descriptions {
  width: 100% !important;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  overflow: hidden;
}

/* 强制使用Element Plus的列布局 */
.custom-descriptions :deep(.el-descriptions__row) {
  display: flex;
  width: 100%;
}

:deep(.my-content) {
  padding: 16px 16px !important;
}

/* 标签列样式 - 左侧蓝灰色背景 */
.custom-descriptions :deep(.el-descriptions__cell--label) {
  width: 150px !important;
  min-width: 150px !important;
  background-color: #f5f7fa !important;
  font-weight: 500;
  color: #606266;
  padding: 22px 20px !important;
  border-right: 1px solid #ebeef5;
  border-bottom: 1px solid #ebeef5;
  text-align: right;
  flex-shrink: 0;
}

/* 内容列样式 - 右侧白色背景 */
.custom-descriptions :deep(.el-descriptions__cell--content) {
  flex: 1;
  padding: 22px 20px !important;
  border-bottom: 1px solid #ebeef5;
  color: #303133;
  min-width: 0;
  word-break: break-all;
}

/* 移除最后一行的底边框 */
.custom-descriptions :deep(.el-descriptions__row:last-child .el-descriptions__cell) {
  border-bottom: none;
}

/* 二维码容器 */
.qrcode-container {
  margin-top: 0;
}

.qrcode-placeholder {
  width: 150px;
  height: 150px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f7fa;
  border: 1px dashed #dcdfe6;
  color: #909399;
}

.qrcode-image {
  width: 150px;
  height: 150px;
  object-fit: contain;
}

/* 对话框底部按钮 */
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>